package estudoregistry;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TesteLeitura {
    final private static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public TesteLeitura() {
        try {
            byte[] b4 = new byte[4];
            byte[] b8 = new byte[8];
            byte[] b16 = new byte[16];
            byte[] b64 = new byte[64];

            FileInputStream fiStream = new FileInputStream("//10.12.56.40/public/dev/NTUSER.DAT");
            fiStream.read(b4);
            System.out.println("Magic number: " + bytesToStringFiltered(b4));

            fiStream.read(b4);
            System.out.println("Sequence number 1: " + unsignedIntFromByteArray(b4));

            fiStream.read(b4);
            System.out.println("Sequence number 2: " + unsignedIntFromByteArray(b4));

            fiStream.read(b8);
            System.out.println("64-bit NT time stamp: " + unsignedIntFromByteArray(b8));

            fiStream.read(b4);
            System.out.println("Major version: " + unsignedIntFromByteArray(b4));

            fiStream.read(b4);
            System.out.println("Minor version: " + unsignedIntFromByteArray(b4));

            fiStream.read(b4);
            System.out.println("Unknown (type?): " + unsignedIntFromByteArray(b4));

            fiStream.read(b4);
            System.out.println("Unknown (format?): " + unsignedIntFromByteArray(b4));

            fiStream.read(b4);
            System.out.println("Pointer (offset) to the first key record: " + bytesToHex(b4));

            fiStream.read(b4);
            System.out.println("Pointer (offset) to the start of last hbin in file: " + bytesToHex(b4));

            fiStream.read(b4);
            System.out.println("Unknown (always 1): " + unsignedIntFromByteArray(b4));

            fiStream.read(b64);
            System.out.println("Hive file name?: " + bytesToStringFiltered(b64));
            
            fiStream.read(b16);
            System.out.println("GUID unknown: " + bytesToHex(b16));

            fiStream.read(b16);
            System.out.println("GUID unknown: " + bytesToHex(b16));

            fiStream.read(b4);
            System.out.println("Unknown (flags?): " + unsignedIntFromByteArray(b4));

            fiStream.read(b16);
            System.out.println("GUID unknown: " + bytesToHex(b16));

            fiStream.read(b4);
            System.out.println("Unknown : " + unsignedIntFromByteArray(b4));

            byte[] b340 = new byte[340];
            fiStream.read(b340);
            System.out.println("Unknown (reserved?) : " + bytesToStringFiltered(b340));

            fiStream.read(b4);
            System.out.println("Checksum of data to this point in header: " + unsignedIntFromByteArray(b4));

            byte[] b3528 = new byte[3528];
            fiStream.read(b3528);
            System.out.println("Unknown (reserved?) : " + bytesToStringFiltered(b3528));

            fiStream.read(b16);
            System.out.println("GUID unknown: " + bytesToHex(b16));

            fiStream.read(b16);
            System.out.println("GUID unknown: " + bytesToHex(b16));

            fiStream.read(b16);
            System.out.println("GUID unknown: " + bytesToHex(b16));

            fiStream.read(b4);
            System.out.println("Unknown: " + bytesToStringFiltered(b4));

            fiStream.read(b4);
            System.out.println("Unknown: " + bytesToStringFiltered(b4));

            fiStream.read(b4);
            System.out.println("Magic number(hbin): " + bytesToStringFiltered(b4));

            fiStream.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TesteLeitura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TesteLeitura.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static int byteArrayToInt(byte[] b) {
        return new BigInteger(b).intValue();
    }

    private static int unsignedIntFromByteArray(byte[] b) {
        int res = 0;
        if (b == null) {
            return res;
        }

        for (int i = 0; i < b.length; i++) {
            res = (res * 10) + ((b[i] & 0xff));
        }
        return res;
    }

    private static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    private static String bytesToStringFiltered(byte[] b) {
        char[] c = new char[tamanhoFiltrado(b)];
        int j=0;
        for (int i = 0; i < b.length; i++) {
            if (b[i]!=0) {
                c[j] = (char) b[i];
                j++;
            }
        }
        return String.valueOf(c);
    }

    private static int tamanhoFiltrado(byte[] b) {
        int resposta = b.length;
        int tamanhoInicial = resposta;

        for (int i=0;i<tamanhoInicial;i++) {
            
            if (b[i]==0x0) {
                resposta--;
            }
        }
        return resposta;
    }
}
